package com.prp.gwt.client.widgets.user.home;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.php.PHPHistoryHandler;
import com.prp.gwt.client.user.php.PHPListener;
import com.prp.gwt.client.vo.Message;
import com.prp.gwt.client.vo.MessageList;

public class MsgBoardRemindPanel extends DecoratorPanel {

	private static MsgBoardRemindPanel instance;
	private FlexTable contentPanel = new FlexTable();
	private Image loadImage = new Image(GWT.getModuleBaseURL() + "loading.gif");
	private VerticalPanel vPanel = new VerticalPanel();

	public static MsgBoardRemindPanel getInstance() {
		return instance;
	}

	public MsgBoardRemindPanel() {
		instance = this;
		vPanel.add(loadImage);
		vPanel.add(contentPanel);
		add(vPanel);
		addStyleName("msgboard-remind");
	}

	public void init() {
		loadImage.setVisible(true);
		contentPanel.clear();
	}

	public void load(final MessageList list) {
		loadImage.setVisible(false);
		if (list.size() == 0) {
			setVisible(false);
			return;
		} else
			setVisible(true);
		for (int i = 0; i < list.size(); i++) {
			final Message msg = list.get(i);
			HorizontalPanel messagePanel = new HorizontalPanel();
			HTML name = new HTML("<a>" + msg.getFromUser().getUsername()
					+ "</a>");
			name.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					History.newItem(PHPHistoryHandler.PREFIX_USERHOME+msg.getFromUser().getId());
				}
			});
			name.addStyleName("msgboard-remind-name");
			messagePanel.add(name);
			messagePanel.add(new Label("在"));
			if (msg.getFromUser().equals(msg.getInUser()))
				messagePanel.add(new Label("他"));
			else if (msg.getInUser().equals(list.getCurrentUser()))
				messagePanel.add(new Label("您"));
			else {
				name = new HTML("<a>" + msg.getInUser().getUsername() + "</a>");
				name.addStyleName("msgboard-remind-name");
				name.addClickHandler(new ClickHandler(){
					@Override
					public void onClick(ClickEvent event) {
						History.newItem(PHPHistoryHandler.PREFIX_USERHOME+msg.getInUser().getId());
					}
				});
				messagePanel.add(name);
			}
			messagePanel.add(new Label("的留言板内给你留言了"));
			HTML look = new HTML("<a>查看</a>");
			look.addStyleName("msgboard-remind-link");
			HTML delete = new HTML("<a>删除</a>");
			delete.addStyleName("msgboard-remind-link");
			look.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					PHPListener.getInstance().setMessageChecked(msg.getId());
					if (msg.getInUser().equals(list.getCurrentUser()))
						History.newItem(PHPHistoryHandler.INIT_VALUE);
					else
						History.newItem(PHPHistoryHandler.PREFIX_USERHOME
								+ msg.getInUser().getId());
				}
			});
			delete.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					PHPListener.getInstance().setMessageChecked(msg.getId());
					MsgBoardRemindPanel.this.init();
					PHPListener.getInstance().getUncheckMessage();
				}
			});
			contentPanel.setWidget(i, 0, messagePanel);
			contentPanel.setWidget(i, 1, look);
			contentPanel.setWidget(i, 2, delete);
			contentPanel.getCellFormatter().addStyleName(i, 0,
					"msgboard-remind-message");
			contentPanel.getCellFormatter().addStyleName(i, 1,
					"msgboard-remind-cell");
			contentPanel.getCellFormatter().addStyleName(i, 2,
					"msgboard-remind-cell");
		}
	}

}
